﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook
{
    public class ObjectTypeManager:ConnectionManager
    {
        public DataTable GetObjects()
        {
            SqlCommand _SQL_SELECT_COMMAND = new SqlCommand();
            SqlDataAdapter _ADAPTER = new SqlDataAdapter();
            DataTable dt = new DataTable();

            _SQL_SELECT_COMMAND.Connection = this.Connection;
            _SQL_SELECT_COMMAND.CommandText = "SELECT DocType ObjType, DocumentName ObjName, 'Full Access' AccessType FROM OBJTYPE";

            _ADAPTER.SelectCommand = _SQL_SELECT_COMMAND;
            _ADAPTER.Fill(dt);

            return dt;
        }

        public DataTable GetObjectTypes()
        {
            SqlCommand _SQL_SELECT_COMMAND = new SqlCommand();
            SqlDataAdapter _ADAPTER = new SqlDataAdapter();
            DataTable dt = new DataTable();

            _SQL_SELECT_COMMAND.Connection = this.Connection;
            _SQL_SELECT_COMMAND.CommandText = "SELECT * FROM OBJTYPE";

            _ADAPTER.SelectCommand = _SQL_SELECT_COMMAND;
            _ADAPTER.Fill(dt);

            return dt;
        }



        public DataTable GetTransactionObjects()
        {
            SqlCommand _SQL_SELECT_COMMAND = new SqlCommand();
            SqlDataAdapter _ADAPTER = new SqlDataAdapter();
            DataTable dt = new DataTable();

            _SQL_SELECT_COMMAND.Connection = this.Connection;
            _SQL_SELECT_COMMAND.CommandText = "SELECT * FROM OBJTYPE WHERE IsTransaction = 1";

            _ADAPTER.SelectCommand = _SQL_SELECT_COMMAND;
            _ADAPTER.Fill(dt);

            return dt;
        }


        public void UpdateReportLayout(DataTable _OBJECTTYPES)
        {
            SqlCommand oCommand = new SqlCommand();
            string _UPDATE_QUERY = "UPDATE OBJTYPE SET PrintLayout1=@PrintLayout1, PrintLayout2=@PrintLayout2, PrintLayout3=@PrintLayout3 WHERE DocType=@DocType";

            foreach (DataRow oRow in _OBJECTTYPES.Rows)
            {
                if (oRow.RowState == DataRowState.Modified)
                {
                    oCommand = new SqlCommand();
                    oCommand.Connection = this.Connection;
                    oCommand.CommandText = _UPDATE_QUERY;

                    oCommand.Parameters.Add(new SqlParameter("@PrintLayout1", oRow["PrintLayout1"]));
                    oCommand.Parameters.Add(new SqlParameter("@PrintLayout2", oRow["PrintLayout2"]));
                    oCommand.Parameters.Add(new SqlParameter("@PrintLayout3", oRow["PrintLayout3"]));

                    oCommand.Parameters.Add(new SqlParameter("@DocType", oRow["DocType"]));

                    oCommand.ExecuteNonQuery();
                }
            }
        }

        public void UpdateSeries(DataTable _OBJECTTYPES)
        {
            SqlCommand oCommand = new SqlCommand();
            string _UPDATE_QUERY = "UPDATE OBJTYPE SET DocExtension=@DocExtension, Series=@Series, IsAuto=@IsAuto WHERE DocType=@DocType";

            foreach (DataRow oRow in _OBJECTTYPES.Rows)
            {
                if (oRow.RowState == DataRowState.Modified)
                {
                    oCommand = new SqlCommand();
                    oCommand.Connection = this.Connection;
                    oCommand.CommandText = _UPDATE_QUERY;

                    oCommand.Parameters.Add(new SqlParameter("@DocExtension", oRow["DocExtension"]));
                    oCommand.Parameters.Add(new SqlParameter("@Series", oRow["Series"]));
                    oCommand.Parameters.Add(new SqlParameter("@IsAuto", oRow["IsAuto"]));

                    oCommand.Parameters.Add(new SqlParameter("@DocType", oRow["DocType"]));

                    oCommand.ExecuteNonQuery();
                }
            }
        }

        public string GetPrintLayout1(int _DocType)
        {
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();
            string _SQL_QUERY = "SELECT PrintLayout1 FROM OBJTYPE WHERE DocType=@DocType";

            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = _SQL_QUERY;
                oCommand.Parameters.Add(new SqlParameter("@DocType", _DocType));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);
                DataRow oRow = dt.Rows[0];

                return oRow["PrintLayout1"].ToString();

            }
            catch(Exception ex)
            {
                throw ex;
            }
        }


        public string GetPrintLayout2(int _DocType)
        {
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();
            string _SQL_QUERY = "SELECT PrintLayout2 FROM OBJTYPE WHERE DocType=@DocType";

            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = _SQL_QUERY;
                oCommand.Parameters.Add(new SqlParameter("@DocType", _DocType));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);
                DataRow oRow = dt.Rows[0];

                return oRow["PrintLayout2"].ToString();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public string GetPrintLayout3(int _DocType)
        {
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();
            string _SQL_QUERY = "SELECT PrintLayout3 FROM OBJTYPE WHERE DocType=@DocType";

            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = _SQL_QUERY;
                oCommand.Parameters.Add(new SqlParameter("@DocType", _DocType));

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);
                DataRow oRow = dt.Rows[0];

                return oRow["PrintLayout3"].ToString();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
